String.prototype.hashCode = function() {
  var hash = 0, i, chr, len;
  if (this.length === 0) return hash;
  for (i = 0, len = this.length; i < len; i++) {
    chr   = this.charCodeAt(i);
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
};

jQuery(document).ready(function() {
  var container, index, list, record, section, url;
  var disqus_shortname = 'karasgollumtest';
  //list = $(".article-content > p," + ".article-content > h2," + ".article-content > h3," + ".article-content > h4," + ".article-content > h5," + ".article-content > h6," + ".article-content > ul," + ".article-content > ol," + ".article-content > blockquote," + ".article-content > dl," + ".article-content > pre," + ".article-content > div");
  list = $(".markdown-body > p," + ".markdown-body > h2," + ".markdown-body > h3," + ".markdown-body > h4," + ".markdown-body > h5," + ".markdown-body > h6," + ".markdown-body > ul," + ".markdown-body > ol," + ".markdown-body > blockquote," + ".markdown-body > dl," + ".markdown-body > pre," + ".markdown-body > div");
  url = window.location.href.replace(window.location.hash, '').replace(/\/index.html?$/i, '/');
  container = $("<aside id=\"inline-comments\"></aside>").appendTo($("#wiki-body"));
  section = 0;
  index = 0;
  record = {};
  list.each(function() {
    var $element, element;
    element = this;
    $element = $(element);
    console.log($element);
    if ($element.prop("tagName") === "H2") {
      ++section;
      return record = {};
    } else {
      return (function() {
        var $commentElement, commentElement, countElement, identifier, tagName;
        tagName = $element.prop("tagName").toLowerCase();
        if (record[tagName] != null) {
          index = record[tagName];
          record[tagName]++;
        } else {
          index = record[tagName] = 1;
        }
        identifier = "section-" + section + "/" + tagName + "-" + index;
        $element.attr("id", identifier);
        $commentElement = $('<div class="inline-comment"></div>');
        commentElement = $commentElement.get(0);
        $commentElement.data("identifier", identifier).css({
          top: element.offsetTop + "px",
          height: element.offsetHeight + "px"
        }).on("mouseenter", function() {
          if (Math.abs(element.offsetTop - commentElement.offsetTop) > 1) {
            $commentElement.css({
              top: element.offsetTop + "px"
            });
          }
          if (Math.abs(element.offsetHeight - commentElement.offsetHeight) > 1) {
            $commentElement.css({
              height: element.offsetHeight + "px"
            });
          }
        });
        $element.on("mouseenter", function() {
          $commentElement.addClass("hover");
          if (Math.abs(element.offsetTop - commentElement.offsetTop) > 1) {
            $commentElement.css({
              top: element.offsetTop + "px"
            });
          }
          if (Math.abs(element.offsetHeight - commentElement.offsetHeight) > 1) {
            $commentElement.css({
              height: element.offsetHeight + "px"
            });
          }
        }).on("mouseleave", function() {
          $commentElement.removeClass("hover");
        });
        countElement = $("<a rel=\"nofollow\" class=\"count\" href=\"" + identifier + "#disqus_thread\" data-disqus-identifier=\"" + url + identifier + "\"></a>").appendTo($commentElement.appendTo(container));
      })();
    }
  });
  container.on("click", ".count", function(e) {
    console.log("container.on method");
    var el, identifier, iframe, parent, title;
    e.preventDefault();
    e.stopPropagation();
    el = $(this);
    parent = el.closest(".inline-comment");
    if (!parent.hasClass("active")) {
      $(document.body).addClass("comment-open");
      container.children(".inline-comment.active").removeClass("active").children("iframe").fadeOut();
      parent.addClass("active");
      if (parent.children("iframe").length === 0) {
        identifier = "" + url + (parent.data("identifier"));
        title = document.title + (" - inline [" + (parent.data("identifier")) + "]");
        iframe = document.createElement('iframe');
        iframe.allowtransparency = true;
        iframe.src = "about:blank";
        parent.get(0).appendChild(iframe);
        iframe.contentWindow.document.open('text/html', 'replace');
        iframe.contentWindow.document.write("<!DOCTYPE html>\n<html>\n<head>\n  <link href=\"//" + window.location.host + "/assets/themes/pure/iframe/iframe.css\" rel=\"stylesheet\" media=\"screen\">\n</head>\n<body>\n  <div id=\"disqus_thread\">\n    <style type=\"text/css\">\n    .disqus-loader, .disqus-loader-wrap{background-image: url();}\n    .disqus-loader{width: 29px; height: 29px; margin: 11px 14px;  background-position: -54px 0px; background-repeat: no-repeat; animation:disqus-embed-spinner .7s infinite linear;-webkit-animation:disqus-embed-spinner .7s infinite linear}@keyframes disqus-embed-spinner{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}@-webkit-keyframes disqus-embed-spinner{0%{-webkit-transform:rotate(0deg)}100%{-webkit-transform:rotate(360deg)}}\n    .disqus-loader-wrap{width: 54px; height: 54px; margin: 0px auto; overflow: hidden; background-repeat: no-repeat;}\n    </style>\n    <div class=\"disqus-loader-wrap\">\n      <div class=\"disqus-loader\">\n      </div>\n    </div>\n  </div>\n  <script>\n  var disqus_developer = " + (window.disqus_developer || 0) + ";\n  var disqus_shortname = \"" + disqus_shortname + "\";\n  var disqus_title = \"" + title + "\";\n  var disqus_url = \"" + identifier + "\";\n  var disqus_identifier = \"" + identifier + "\";\n  (function() {\n      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;\n      dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';\n      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);\n  })();\n  </script>\n  <script src=\"//" + window.location.host + "/assets/themes/pure/iframe/iframeResizer.contentWindow.js\"></script>\n</body>\n</html>");
        iframe.contentWindow.document.close();
        return $(iframe).fadeIn().iFrameResize({
          log: false,
          autoResize: true,
          contentWindowBodyMargin: 0,
          doHeight: true,
          doWidth: false,
          enablePublicMethods: false,
          interval: 100,
          scrolling: false
        });
      } else {
        return parent.children("iframe").fadeIn();
      }
    } else {
      $(document.body).removeClass("comment-open");
      parent.removeClass("active");
      return parent.children("iframe").fadeOut();
    }
  });
  return (function() {
    var s;
    s = document.createElement('script');
    s.async = true;
    s.type = 'text/javascript';
    s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
    return (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
  })();
});